'''
Created on Oct 11, 2012

@author: daeheehan

Modified on Oct 31, 2012
@author: gyucheol
'''

from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
from pprint import pprint
import sys
from sets import Set

# Function definition is here
def removeDuplicate(seq):
  seen = set()
  seen_add = seen.add
  # adds all elements it doesn't know yet to seen and all other to seen_twice
  seen_twice = set( x for x in seq if x in seen or seen_add(x) )
  # turn the set into a list (as requested)
  return list( seen_twice )

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"
GITHUB_USER_ID_3 = 'skswhwo'
GITHUB_USER_ID_4 = 'luispigu'

GITHUB_USER_PW = "qwer1234"
GITHUB_USER_PW_1 = "wasd1234"

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID, password=GITHUB_USER_PW)
print gh.ratelimit_remaining, 'request remaining'

# Github Rails Repository information
GITHUB_USER_NAME = "rails"
GITHUB_REPOSITORY_NAME = "rails"

# Rails changer commenter information file
GRAPH_FILE = 'rails_changer_commenters.txt'

# Rails refined changer commenter information file
GRAPH_REFINED_FILE = 'rails_refined_changer_commenters.txt'

# Rails pullrequester follower information file
FOLLOWER_FILE = 'rails_refined_pullrequester_follower.txt'

list_dup_pullrequester = []
list_uni_pullrequester = []
pullrequester_followers = {}

try:
    # Rails project pull requester list from files
    f_pullrequester = open(GRAPH_REFINED_FILE, 'r')
    
    for line in f_pullrequester:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            if(tuple_list[0].strip() == 'Id'):
                continue
            
            login = tuple_list[3].strip()
#            start_offset = login.find(':')
#            user_login = login[start_offset+1:].strip()
#            print user_login
            list_dup_pullrequester.append(login)

    f_pullrequester.close()
    
    print 'Pull Requester loading finished'
    print len(list_dup_pullrequester)
    list_uni_pullrequester = removeDuplicate(list_dup_pullrequester)
    list_uni_pullrequester.sort()
    print len(list_uni_pullrequester)
    
    for item in list_uni_pullrequester:
#        print item
        user = gh.user(item)
        print user.login
        
        list_followers = []
        try:
            for follower in user.iter_followers():
    #            print follower.login
                list_followers.append(', ' + follower.login)
                
                if(gh.ratelimit_remaining < 100):
                    time.sleep(60 * 60)
        except Exception as e:
            print 'The final Exception', e.args[0]
            follower_set = ''
        finally:
            follower_set = ''.join(list_followers)
    #        print follower_set
            pullrequester_followers[item] = follower_set

#        break

    print 'Printing dictionary: Followers'
    f_followers = open(FOLLOWER_FILE, 'w')
    f_followers.write('Pull Requester, Followers \n')
    for key, value in sorted(pullrequester_followers.items()):
        sys.stdout.write(str(key) + str(value) + '\n')
        f_followers.write(str(key) + str(value) + '\n')

    f_followers.close()

except Exception as e: 
    print 'The final Exception', e.args[0]

print gh.ratelimit_remaining, 'request remaining'

print
print 'Finally the end'
